package top.blackr.folivora.common.security.auth;

import cn.dev33.satoken.stp.StpUtil;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import top.blackr.folivora.common.core.R;
import top.blackr.folivora.common.core.constants.CommonConstants;
import top.blackr.folivora.common.security.auth.impl.LoginService;

@AllArgsConstructor
@RestController("login")
public class LoginController {

    private LoginAuthenticationFactory loginAuthenticationFactory;

    /**
     * 统一登录入口
     *
     * @param authentication 请求参数
     * @return 登录信息
     */
    @PostMapping(CommonConstants.LOGIN_URL)
    @Operation(summary = "登录多合一", description = "根据loginType，区分登录类型")
    public R<SecUser> auth(@RequestBody LoginAuthentication authentication) {
        // 交给指定登录类型去处理
        LoginService loginService = loginAuthenticationFactory.getLoginService(authentication.getLoginType());
        // 获取登录信息
        return R.ok(loginService.login(loginService, authentication));
    }

    @PostMapping("logout")
    @Operation(summary = "退出登录")
    public R<Boolean> logout() {
        StpUtil.logout();
        return R.ok(Boolean.TRUE);
    }
}
